package cn.edu.sdjzu.xg.kcsj.service;

import cn.edu.sdjzu.xg.kcsj.dao.DictItemDao;
import cn.edu.sdjzu.xg.kcsj.domain.DictItem;
import cn.edu.sdjzu.xg.kcsj.domain.Dictionary;
import cn.edu.sdjzu.xg.kcsj.exception.BysjException;
import cn.edu.sdjzu.xg.kcsj.util.JdbcHelper;
import cn.edu.sdjzu.xg.kcsj.util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;
import java.util.TreeSet;

public class DictItemService {
    private static DictItemDao dictItemDao = DictItemDao.getInstance();
    private static DictItemService dictItemService = new DictItemService();

    public static DictItemService getInstance(){
        return dictItemService;
    }

    // 查找全部院系
    public Collection<DictItem> findAll(List conditionList, Pagination pagination) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        Collection<DictItem> dictItems = new TreeSet<>();
        try {
            dictItems = dictItemDao.findAll(conditionList,pagination,connection);
            //提交事务
            connection.commit();
        }catch (SQLException e){
            //回滚
            connection.rollback();
        } finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return dictItems;
    }

    // 按id查找院系
    public DictItem find(Integer id) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        connection.setAutoCommit(false);
        DictItem dictItem = null;
        try {
            dictItem = dictItemDao.find(id,connection);
            connection.commit();
        }catch (SQLException e){
            connection.rollback();
        }finally {
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return dictItem;
    }

    // 更新一条
    public boolean update(DictItem dictItem) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        connection.setAutoCommit(false);
        Boolean updated =false;
        try {
            updated = dictItemDao.update(dictItem,connection);
            connection.commit();
        }catch (SQLException e){
            connection.rollback();
        }finally {
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return updated;
    }

    public boolean add(DictItem dictItem) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        connection.setAutoCommit(false);
        Boolean added =false;
        try {
            added = dictItemDao.add(dictItem,connection);
            connection.commit();
        }catch (SQLException e){
            connection.rollback();
        }finally {
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return added ;
    }

    // 删除
    public boolean delete(Integer id) throws SQLException, BysjException {
        Connection connection = JdbcHelper.getConn();
        connection.setAutoCommit(false);
        Boolean deleted = false;
        try {
            deleted = dictItemDao.delete(id,connection);
            connection.commit();
        } catch (SQLException e) {
            connection.rollback();
        } finally {
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return deleted;
    }

    public Collection<DictItem> findAllWithoutElse() throws SQLException,BysjException{
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        Collection<DictItem> dictItems = new TreeSet<>();
        try {
            dictItems = dictItemDao.findAllWithoutElse(connection);
            //提交事务
            connection.commit();
        }catch (SQLException e){
            //回滚
            connection.rollback();
        } finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return dictItems;
    }
}
